#include "../stdafx.h"
#include "WorldMap.h"
#include "Layer.h"
#include "Tile.h"
#include "../Game.h"

CWorldMap::CWorldMap(const char* imageFilePath)
{
	m_hTileSet = SGD::GraphicsManager::GetInstance()->LoadTexture(imageFilePath);
}

CWorldMap::~CWorldMap()
{
	for (unsigned int i = 0; i < m_vLayers.size(); i++)
		delete m_vLayers[i];
	m_vLayers.clear();

	for (unsigned int i = 0; i < m_vTiles.size(); i++)
		delete m_vTiles[i];
	m_vTiles.clear();
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hTileSet);
}

void CWorldMap::Render()
{
	for (unsigned int i = 0; i < m_vLayers.size(); i++)
	{
		if (m_vLayers[i] != nullptr)
			m_vLayers[i]->Render();
	}

	SGD::Rectangle tempCam(Game::GetInstance()->GetCameraPos().x, Game::GetInstance()->GetCameraPos().y,
		Game::GetInstance()->GetCameraPos().x + Game::GetInstance()->GetScreenWidth(),
		Game::GetInstance()->GetCameraPos().y + Game::GetInstance()->GetScreenHeight());
	for (unsigned int i = 0; i < m_vTiles.size(); i++)
	{
		if (m_vTiles[i] != nullptr && m_vTiles[i]->GetRect().IsIntersecting(tempCam))
		{
			SGD::Rectangle tempRect(m_vTiles[i]->GetSource(), m_szTileSize);
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(
				m_hTileSet, SGD::Point(m_vTiles[i]->GetRect().left - Game::GetInstance()->GetCameraPos().x, m_vTiles[i]->GetRect().top - Game::GetInstance()->GetCameraPos().y),
				tempRect);
		}
	}
}

void CWorldMap::Update(float dt)
{
	for (unsigned int i = 0; i < m_vLayers.size(); i++)
	{
		if (m_vLayers[i] != nullptr)
			m_vLayers[i]->Update(dt);
	}
}



